package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.RegUser;
import com.woniu.mapper.RegUserMapper;
import com.woniu.service.RegUserService;
import com.woniu.util.TopicPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Classname RegUserServiceImpl
 * @Description TODO
 * @Date 2025/10/24 15:08
 * @Created by pc
 */
@Service
public class RegUserServiceImpl extends ServiceImpl<RegUserMapper,RegUser> implements RegUserService {
    @Resource
    private RegUserMapper regUserMapper;
    @Resource
    private TopicPublisher topicPublisher;
    @Override
    public Map<String, Object> reg(RegUser regUser) {
        // 注册 生成激活码
        String code = UUID.randomUUID().toString().replace("-", "");
        regUser.setCode(code);
        // 发送验证码
        String msg = regUser.getPhone() + ":" + code;
        topicPublisher.send(msg,"regcode");
        regUser.setStatus(0);
        // 插入数据库
        int insert = regUserMapper.insert(regUser);
        Map<String,Object> map = new HashMap<>();
        if(insert > 0){
            map.put("status",200);
            map.put("msg","注册成功");
        }else{
            map.put("status",500);
            map.put("msg","注册失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> active(RegUser regUser) {
        // 激活操作
        // 先查询是否存在该用户 并且是否是未激活状态
        QueryWrapper<RegUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",regUser.getUsername());
        RegUser user = regUserMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        if(user == null){
            map.put("status",500);
            map.put("msg","用户不存在");
            return map;
        }
        if(user.getStatus() == 1){
            map.put("status",500);
            map.put("msg","用户已激活");
            return map;
        }
        // 激活操作 判断激活码是否正确
        if(!user.getCode().equals(regUser.getCode())){
            map.put("status",500);
            map.put("msg","激活码错误");
            return map;
        }
        // 激活成功
        user.setStatus(1);  // 可以不要  依据code 有没有值来决定是否激活  激活成功 再code
        user.setCode(null);
        int update = regUserMapper.update(user, null);
        if(update > 0){
            map.put("status",200);
            map.put("msg","激活码成功");
        }else{
            map.put("status",500);
            map.put("msg","激活码失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> login(RegUser regUser) {
        QueryWrapper<RegUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",regUser.getUsername());
        RegUser loginUser = regUserMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        if(loginUser == null){
            map.put("status",500);
            map.put("msg","用户不存在");
        }else if(loginUser.getStatus() == 0){
            map.put("status",500);
            map.put("msg","用户未激活不能登录");
        }else if(!loginUser.getPassword().equals(regUser.getPassword())){
            map.put("status",500);
            map.put("msg","密码错误");
        }else{
            map.put("status",200);
            map.put("msg","登录成功");
        }
        return map;
    }
}
